//
//  LKDayInformationDetailViewController.m
//  YLHelper
//
//  Created by she1323 on 2018/4/19.
//  Copyright © 2018年 she1323. All rights reserved.
//

#import "LKDayInformationDetailViewController.h"
#import "LKDayInforDetailHeaderCell.h"
#import "LKDayInforDetailBottomCell.h"
#import "LKDayInformationModel.h"
#import <UMShare/UMShare.h>
#import <UShareUI/UShareUI.h>
#import "LKDayInforPersonInforEditViewController.h"
@interface LKDayInformationDetailViewController ()<UITableViewDataSource,UITableViewDelegate,UIWebViewDelegate>
@property(nonatomic,strong)LKBaseTableview *tableview;
@property(nonatomic,strong)LKDayInformationModel *dayInforModel;
@property(nonatomic,assign) CGFloat webviewHeight;
@property(nonatomic,strong)UIWebView *webView;
@end

@implementation LKDayInformationDetailViewController
-(LKBaseTableview *)tableview{
    if (!_tableview) {
        _tableview = [LKBaseTableview setTableviewWithFrame:CGRectMake(0, NAVIGATION_BAR_HEIGHT, WIDTH, 225) style:UITableViewStyleGrouped registercellClasss:@[@"LKDayInforDetailHeaderCell",@"LKDayInforDetailBottomCell"]];
        [self.view addSubview:_tableview];
        _tableview.delegate = self;
        _tableview.dataSource = self;
        _tableview.estimatedRowHeight = 500;
    }
    return _tableview;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LKDayInforDetailHeaderCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LKDayInforDetailHeaderCell"];
    [[cell.editBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        LKDayInforPersonInforEditViewController *vc = [[LKDayInforPersonInforEditViewController alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
    }];
    switch (indexPath.section) {
        case 1:
        {
            LKDayInforDetailBottomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LKDayInforDetailBottomCell"];
//            self.webView.autoresizingMask = (UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight);
//            self.webView.scalesPageToFit = YES;
//            [self.webView setScalesPageToFit:YES];
            [cell.bgview addSubview:self.webView];
            return cell;
        }
            break;
        default:
            break;
    }
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return indexPath.section == 0 ? 225:self.webviewHeight;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return section == 0 ? 0.00000000001:12;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.00000000001;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [[UIView alloc] init];
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = cellHeaderBgColor;
    return view;
}
-(void)webViewDidFinishLoad:(UIWebView *)webView{
    if (self.webviewHeight == 0) {
          self.webviewHeight = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight"] floatValue]+20 ;
           [self.tableview reloadSection:1 withRowAnimation:UITableViewAutomaticDimension];
    }else{
        
    }

//    [self.webView reload];
//    [self.tableview reloadSection:1 withRowAnimation:UITableViewAutomaticDimension];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.webviewHeight = 0;
    [self setupLeftBaritem];
    [self createWebView];
//    [self.tableview reloadData];
    [self getDayNewsDetailInforamtin];
    NSLog(@"部门名称%@",[LKUserInforTool account].departmentName);

    // Do any additional setup after loading the view.
}
-(void)setupLeftBaritem{
     self.navigationItem.title = @"每日资讯";
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(0, 0, 30, 30);
    [btn setImage:[UIImage imageNamed:@"share"] forState:UIControlStateNormal];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
    [[btn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        NSLog(@"分享");
        
        [UMSocialUIManager setPreDefinePlatforms:@[@(UMSocialPlatformType_WechatSession),@(UMSocialPlatformType_WechatTimeLine),@(UMSocialPlatformType_QQ),@(UMSocialPlatformType_Qzone),@(UMSocialPlatformType_Sms)]];
        [UMSocialUIManager showShareMenuViewInWindowWithPlatformSelectionBlock:^(UMSocialPlatformType platformType, NSDictionary *userInfo) {
        
        }];
    }];
}

/**
 获取每日资讯的详情
 */
-(void)getDayNewsDetailInforamtin{
    NSDictionary *param = @{@"Id":self.Id};
    [[LKNetDataManager defaultManager] sendRequestMethod:HTTPMethodPOST serverUrl:ServerlUrl apiPath:getDayInformationDetailUrl header:[LKUserInforTool header] parameters:param progress:^(NSProgress * _Nullable progress) {
        
    } success:^(BOOL isSuccess, id  _Nullable responseObject) {
        self.dayInforModel = [LKDayInformationModel modelWithDictionary:responseObject[@"result"]];
//        self.webviewHeight =
        [self.tableview reloadData];
    } failure:^(NSString * _Nullable errorMessage) {
        
    }];
}
- (void)shareWebPageToPlatformType:(UMSocialPlatformType)platformType
{
    //创建分享消息对象
    UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
    //创建网页内容对象
    NSString* thumbURL =  @"https://mobile.umeng.com/images/pic/home/social/img-1.png";
    UMShareWebpageObject *shareObject = [UMShareWebpageObject shareObjectWithTitle:@"欢迎使用【友盟+】社会化组件U-Share" descr:@"欢迎使用【友盟+】社会化组件U-Share，SDK包最小，集成成本最低，助力您的产品开发、运营与推广！" thumImage:thumbURL];
    //设置网页地址
    shareObject.webpageUrl = @"http://mobile.umeng.com/social";
    //分享消息对象设置分享内容对象
    messageObject.shareObject = shareObject;
    //调用分享接口
    [[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:self completion:^(id data, NSError *error) {
        if (error) {
            UMSocialLogInfo(@"************Share fail with error %@*********",error);
        }else{
            if ([data isKindOfClass:[UMSocialShareResponse class]]) {
                UMSocialShareResponse *resp = data;
                //分享结果消息
                UMSocialLogInfo(@"response message is %@",resp.message);
                //第三方原始返回的数据
                UMSocialLogInfo(@"response originalResponse data is %@",resp.originalResponse);
            }else{
                UMSocialLogInfo(@"response data is %@",data);
            }
        }
    }];
}

/**
 设置webview
 */
-(void)createWebView{
    self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 20, WIDTH, HEIGHT - 225)];
    self.webView.delegate = self;
    [self.webView loadHTMLString:self.dayInforModel.informationContent baseURL:nil];
    [self.view addSubview:self.webView];

}
//-(UIWebView *)webView{
//    if (!_webView) {
//        _webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 20, WIDTH, self.webviewHeight)];
//        _webView.delegate = self;
//    }
//    return _webView;
//}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
